﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace LynxWebLog.Areas.Blogs.ViewModels
{
    public class PostEntry
    {
        public PostData pd { get; set; }
        public string categoryString { get; set; }
        public string dateCreatedString { get; set; }

        public PostEntry() { }
        public PostEntry(PostData pd)
        {
            this.pd = pd;
        }
        public async Task FillCategories()
        {
            HttpClient client = ClientUtility.BlogAPIClient();
            HttpResponseMessage resp = await client.GetAsync("api/PostCategory/" + pd.postid);
            resp.EnsureSuccessStatusCode();

            List<PostCategory> lpc = (await resp.Content.ReadAsAsync<IEnumerable<PostCategory>>()).ToList();
            List<string> lcat = new List<string>();
            foreach (PostCategory item in lpc) {
                resp = await client.GetAsync("api/CategoryData/" + item.category);
                resp.EnsureSuccessStatusCode();
                CategoryData cd = (await resp.Content.ReadAsAsync<IEnumerable<CategoryData>>()).FirstOrDefault();
                if (cd != null) {
                    lcat.Add(cd.title);
                }
            }
            pd.categories = lcat.ToArray();
        }
    }
}